{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.146268900Z",
     "start_time": "2025-09-11T01:55:10.941362300Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [],
   "source": [
    "data = [[\"Mark\", 55, \"Italy\", 4.5, \"Europe\"],\n",
    "        [\"John\", 33, \"USA\", 6.7, \"America\"],\n",
    "        [\"Tim\", 41, \"USA\", 3.9, \"America\"],\n",
    "        [\"Jenny\", 12, \"Germany\", 9.0, \"Europe\"]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.147268400Z",
     "start_time": "2025-09-11T01:55:10.957352300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "outputs": [],
   "source": [
    "df = pd.DataFrame(data=data,\n",
    "                  columns=[\"name\", \"age\", \"country\",\n",
    "                           \"score\", \"continent\"],\n",
    "                  index=[1001, 1000, 1002, 1003])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.147268400Z",
     "start_time": "2025-09-11T01:55:10.970346100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [
    {
     "data": {
      "text/plain": "       name  age  country  score continent\n1001   Mark   55    Italy    4.5    Europe\n1000   John   33      USA    6.7   America\n1002    Tim   41      USA    3.9   America\n1003  Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.148267500Z",
     "start_time": "2025-09-11T01:55:10.979341Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 4 entries, 1001 to 1003\n",
      "Data columns (total 5 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   name       4 non-null      object \n",
      " 1   age        4 non-null      int64  \n",
      " 2   country    4 non-null      object \n",
      " 3   score      4 non-null      float64\n",
      " 4   continent  4 non-null      object \n",
      "dtypes: float64(1), int64(1), object(3)\n",
      "memory usage: 192.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.148267500Z",
     "start_time": "2025-09-11T01:55:11.008323700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "outputs": [
    {
     "data": {
      "text/plain": "name          object\nage            int64\ncountry       object\nscore        float64\ncontinent     object\ndtype: object"
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.150267300Z",
     "start_time": "2025-09-11T01:55:11.040305900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "outputs": [
    {
     "data": {
      "text/plain": "Index([1001, 1000, 1002, 1003], dtype='int64')"
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.150267300Z",
     "start_time": "2025-09-11T01:55:11.072287400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "outputs": [],
   "source": [
    "df.index.name = 'user_id'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.167257400Z",
     "start_time": "2025-09-11T01:55:11.100270800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1001      Mark   55    Italy    4.5    Europe\n1000      John   33      USA    6.7   America\n1002       Tim   41      USA    3.9   America\n1003     Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.168256300Z",
     "start_time": "2025-09-11T01:55:11.124258100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id   name  age  country  score continent\n0     1001   Mark   55    Italy    4.5    Europe\n1     1000   John   33      USA    6.7   America\n2     1002    Tim   41      USA    3.9   America\n3     1003  Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.169255800Z",
     "start_time": "2025-09-11T01:55:11.149243300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "outputs": [
    {
     "data": {
      "text/plain": "       user_id  age  country  score continent\nname                                         \nMark      1001   55    Italy    4.5    Europe\nJohn      1000   33      USA    6.7   America\nTim       1002   41      USA    3.9   America\nJenny     1003   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>name</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Mark</th>\n      <td>1001</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>John</th>\n      <td>1000</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>Tim</th>\n      <td>1002</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>Jenny</th>\n      <td>1003</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.reset_index().set_index('name')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.169255800Z",
     "start_time": "2025-09-11T01:55:11.179226100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "outputs": [],
   "source": [
    "df.index.name = 'user_id'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.170255300Z",
     "start_time": "2025-09-11T01:55:11.211208Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1001      Mark   55    Italy    4.5    Europe\n1000      John   33      USA    6.7   America\n1002       Tim   41      USA    3.9   America\n1003     Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.171255Z",
     "start_time": "2025-09-11T01:55:11.225198800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "outputs": [
    {
     "data": {
      "text/plain": "         name   age country  score continent\nuser_id                                     \n999       NaN   NaN     NaN    NaN       NaN\n1000     John  33.0     USA    6.7   America\n1001     Mark  55.0   Italy    4.5    Europe\n1004      NaN   NaN     NaN    NaN       NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>999</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33.0</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55.0</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1004</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.reindex([999, 1000, 1001, 1004])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.171255Z",
     "start_time": "2025-09-11T01:55:11.263177Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1001      Mark   55    Italy    4.5    Europe\n1000      John   33      USA    6.7   America\n1002       Tim   41      USA    3.9   America\n1003     Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.172254100Z",
     "start_time": "2025-09-11T01:55:11.326141Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1000      John   33      USA    6.7   America\n1001      Mark   55    Italy    4.5    Europe\n1002       Tim   41      USA    3.9   America\n1003     Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.173253800Z",
     "start_time": "2025-09-11T01:55:11.385111200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1000      John   33      USA    6.7   America\n1002       Tim   41      USA    3.9   America\n1003     Jenny   12  Germany    9.0    Europe\n1001      Mark   55    Italy    4.5    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(['continent', 'age'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.173253800Z",
     "start_time": "2025-09-11T01:55:11.417492800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1000      John   33      USA    6.7   America\n1002       Tim   41      USA    3.9   America\n1001      Mark   55    Italy    4.5    Europe\n1003     Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values('continent')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.174253200Z",
     "start_time": "2025-09-11T01:55:11.464548100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['name', 'age', 'country', 'score', 'continent'], dtype='object')"
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.176252700Z",
     "start_time": "2025-09-11T01:55:11.494486500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1001      Mark   55    Italy    4.5    Europe\n1000      John   33      USA    6.7   America\n1002       Tim   41      USA    3.9   America\n1003     Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.177251900Z",
     "start_time": "2025-09-11T01:55:11.526897500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "outputs": [],
   "source": [
    "df.index.name = 'user_id'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.178251600Z",
     "start_time": "2025-09-11T01:55:11.557878400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1001      Mark   55    Italy    4.5    Europe\n1000      John   33      USA    6.7   America\n1002       Tim   41      USA    3.9   America\n1003     Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.179251Z",
     "start_time": "2025-09-11T01:55:11.574868700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "outputs": [
    {
     "data": {
      "text/plain": "          name  age  country  score continent\nuser_id                                      \n1001      Mark   55    Italy    4.5    Europe\n1000      John   33      USA    6.7   America\n1002       Tim   41      USA    3.9   America\n1003     Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.179251Z",
     "start_time": "2025-09-11T01:55:11.607848500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "outputs": [],
   "source": [
    "df.columns.name = 'properties'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.180250500Z",
     "start_time": "2025-09-11T01:55:11.649824900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent\nuser_id                                         \n1001         Mark   55    Italy    4.5    Europe\n1000         John   33      USA    6.7   America\n1002          Tim   41      USA    3.9   America\n1003        Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.180250500Z",
     "start_time": "2025-09-11T01:55:11.670812700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "outputs": [
    {
     "data": {
      "text/plain": "properties First Name  Age  country  score continent\nuser_id                                             \n1001             Mark   55    Italy    4.5    Europe\n1000             John   33      USA    6.7   America\n1002              Tim   41      USA    3.9   America\n1003            Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>First Name</th>\n      <th>Age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.rename(columns={\"name\": \"First Name\", \"age\": \"Age\"})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.181249600Z",
     "start_time": "2025-09-11T01:55:11.699719100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  age  score continent\nuser_id                         \n1001         55    4.5    Europe\n1002         41    3.9   America",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>age</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>55</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>41</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(columns=[\"name\", \"country\"],\n",
    "        index=[1000, 1003])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.182249400Z",
     "start_time": "2025-09-11T01:55:11.747691500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent\nuser_id                                         \n1001         Mark   55    Italy    4.5    Europe\n1000         John   33      USA    6.7   America\n1002          Tim   41      USA    3.9   America\n1003        Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.183254600Z",
     "start_time": "2025-09-11T01:55:11.776674800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "outputs": [
    {
     "data": {
      "text/plain": "user_id       1001     1000     1002     1003\nproperties                                   \nname          Mark     John      Tim    Jenny\nage             55       33       41       12\ncountry      Italy      USA      USA  Germany\nscore          4.5      6.7      3.9      9.0\ncontinent   Europe  America  America   Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>user_id</th>\n      <th>1001</th>\n      <th>1000</th>\n      <th>1002</th>\n      <th>1003</th>\n    </tr>\n    <tr>\n      <th>properties</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>name</th>\n      <td>Mark</td>\n      <td>John</td>\n      <td>Tim</td>\n      <td>Jenny</td>\n    </tr>\n    <tr>\n      <th>age</th>\n      <td>55</td>\n      <td>33</td>\n      <td>41</td>\n      <td>12</td>\n    </tr>\n    <tr>\n      <th>country</th>\n      <td>Italy</td>\n      <td>USA</td>\n      <td>USA</td>\n      <td>Germany</td>\n    </tr>\n    <tr>\n      <th>score</th>\n      <td>4.5</td>\n      <td>6.7</td>\n      <td>3.9</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>continent</th>\n      <td>Europe</td>\n      <td>America</td>\n      <td>America</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.200238900Z",
     "start_time": "2025-09-11T01:55:11.805657700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "outputs": [
    {
     "data": {
      "text/plain": "<bound method DataFrame.transform of properties   name  age  country  score continent\nuser_id                                         \n1001         Mark   55    Italy    4.5    Europe\n1000         John   33      USA    6.7   America\n1002          Tim   41      USA    3.9   America\n1003        Jenny   12  Germany    9.0    Europe>"
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.transform"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.203235900Z",
     "start_time": "2025-09-11T01:55:11.841637200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "outputs": [
    {
     "data": {
      "text/plain": "properties continent  country   name  age  score\nuser_id                                         \n1001          Europe    Italy   Mark   55    4.5\n1000         America      USA   John   33    6.7\n1002         America      USA    Tim   41    3.9\n1003          Europe  Germany  Jenny   12    9.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>continent</th>\n      <th>country</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Europe</td>\n      <td>Italy</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>America</td>\n      <td>USA</td>\n      <td>John</td>\n      <td>33</td>\n      <td>6.7</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>America</td>\n      <td>USA</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>3.9</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Europe</td>\n      <td>Germany</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>9.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[:, [\"continent\", \"country\", \"name\", \"age\", \"score\"]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.204236800Z",
     "start_time": "2025-09-11T01:55:11.874619Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent\nuser_id                                         \n1001         Mark   55    Italy    4.5    Europe\n1003        Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[df['country'].isin(['Italy', 'Germany'])]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.205235700Z",
     "start_time": "2025-09-11T01:55:11.904601200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "outputs": [],
   "source": [
    "# 当作以毫米为单位的年降雨量\n",
    "rainfall = pd.DataFrame(data={\"City 1\": [300.1, 100.2],\n",
    "                              \"City 2\": [400.3, 300.4],\n",
    "                              \"City 3\": [1000.5, 1100.6]})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.205235700Z",
     "start_time": "2025-09-11T01:55:11.937582500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3\n0   300.1   400.3  1000.5\n1   100.2   300.4  1100.6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>300.1</td>\n      <td>400.3</td>\n      <td>1000.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>100.2</td>\n      <td>300.4</td>\n      <td>1100.6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.222226700Z",
     "start_time": "2025-09-11T01:55:11.951574800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3\n0    True   False   False\n1    True    True   False",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>True</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>True</td>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall < 400"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.223225800Z",
     "start_time": "2025-09-11T01:55:11.983555100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3\n0   300.1     NaN     NaN\n1   100.2   300.4     NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>300.1</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>100.2</td>\n      <td>300.4</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall[rainfall < 400]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.226222500Z",
     "start_time": "2025-09-11T01:55:12.016537100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "outputs": [],
   "source": [
    "df_multi = df.reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.227222500Z",
     "start_time": "2025-09-11T01:55:12.047519600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  user_id   name  age  country  score continent\n0              1001   Mark   55    Italy    4.5    Europe\n1              1000   John   33      USA    6.7   America\n2              1002    Tim   41      USA    3.9   America\n3              1003  Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.228222100Z",
     "start_time": "2025-09-11T01:55:12.060511400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent\nuser_id                                         \n1001         Mark   55    Italy    4.5    Europe\n1000         John   33      USA    6.7   America\n1002          Tim   41      USA    3.9   America\n1003        Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.229221700Z",
     "start_time": "2025-09-11T01:55:12.092492800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "outputs": [
    {
     "data": {
      "text/plain": "properties         user_id   name  age  score\ncontinent country                            \nEurope    Italy       1001   Mark   55    4.5\nAmerica   USA         1000   John   33    6.7\n          USA         1002    Tim   41    3.9\nEurope    Germany     1003  Jenny   12    9.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>continent</th>\n      <th>country</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Europe</th>\n      <th>Italy</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">America</th>\n      <th>USA</th>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>6.7</td>\n    </tr>\n    <tr>\n      <th>USA</th>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>3.9</td>\n    </tr>\n    <tr>\n      <th>Europe</th>\n      <th>Germany</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>9.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi.set_index(['continent', 'country'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.230221100Z",
     "start_time": "2025-09-11T01:55:12.125474800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "outputs": [],
   "source": [
    "df_multi = df_multi.sort_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.231219600Z",
     "start_time": "2025-09-11T01:55:12.173447500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  user_id   name  age  country  score continent\n0              1001   Mark   55    Italy    4.5    Europe\n1              1000   John   33      USA    6.7   America\n2              1002    Tim   41      USA    3.9   America\n3              1003  Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.231219600Z",
     "start_time": "2025-09-11T01:55:12.188439Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "outputs": [
    {
     "data": {
      "text/plain": "properties         user_id   name  age  score\ncontinent country                            \nAmerica   USA         1000   John   33    6.7\n          USA         1002    Tim   41    3.9\nEurope    Germany     1003  Jenny   12    9.0\n          Italy       1001   Mark   55    4.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>continent</th>\n      <th>country</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">America</th>\n      <th>USA</th>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>6.7</td>\n    </tr>\n    <tr>\n      <th>USA</th>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>3.9</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">Europe</th>\n      <th>Germany</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>Italy</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 待排序的MultiIndex\n",
    "df_multi = df.reset_index().set_index([\"continent\", \"country\"])\n",
    "df_multi = df_multi.sort_index()\n",
    "df_multi"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.232220100Z",
     "start_time": "2025-09-11T01:55:12.221419900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "outputs": [],
   "source": [
    "df_multi = df.reset_index().set_index([\"continent\", \"country\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.233219100Z",
     "start_time": "2025-09-11T01:55:12.254194200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "outputs": [
    {
     "data": {
      "text/plain": "properties         user_id   name  age  score\ncontinent country                            \nEurope    Italy       1001   Mark   55    4.5\nAmerica   USA         1000   John   33    6.7\n          USA         1002    Tim   41    3.9\nEurope    Germany     1003  Jenny   12    9.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>continent</th>\n      <th>country</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Europe</th>\n      <th>Italy</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">America</th>\n      <th>USA</th>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>6.7</td>\n    </tr>\n    <tr>\n      <th>USA</th>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>3.9</td>\n    </tr>\n    <tr>\n      <th>Europe</th>\n      <th>Germany</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>9.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.233219100Z",
     "start_time": "2025-09-11T01:55:12.284178Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "outputs": [],
   "source": [
    "df_multi = df_multi.sort_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.234217800Z",
     "start_time": "2025-09-11T01:55:12.314160500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "outputs": [
    {
     "data": {
      "text/plain": "properties         user_id   name  age  score\ncontinent country                            \nAmerica   USA         1000   John   33    6.7\n          USA         1002    Tim   41    3.9\nEurope    Germany     1003  Jenny   12    9.0\n          Italy       1001   Mark   55    4.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>continent</th>\n      <th>country</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">America</th>\n      <th>USA</th>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>6.7</td>\n    </tr>\n    <tr>\n      <th>USA</th>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>3.9</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">Europe</th>\n      <th>Germany</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>Italy</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.250209400Z",
     "start_time": "2025-09-11T01:55:12.345142400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  user_id   name  age  score\ncountry                               \nGermany        1003  Jenny   12    9.0\nItaly          1001   Mark   55    4.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>country</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Germany</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>Italy</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi.loc['Europe', :]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.250209400Z",
     "start_time": "2025-09-11T01:55:12.374125700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "outputs": [
    {
     "data": {
      "text/plain": "properties         user_id  name  age  score\ncontinent country                           \nEurope    Italy       1001  Mark   55    4.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>continent</th>\n      <th>country</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Europe</th>\n      <th>Italy</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi.loc[('Europe', 'Italy')]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.251208500Z",
     "start_time": "2025-09-11T01:55:12.407107900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "outputs": [
    {
     "data": {
      "text/plain": "properties         user_id   name  age  score\ncontinent country                            \nAmerica   USA         1000   John   33    6.7\n          USA         1002    Tim   41    3.9\nEurope    Germany     1003  Jenny   12    9.0\n          Italy       1001   Mark   55    4.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>properties</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>continent</th>\n      <th>country</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">America</th>\n      <th>USA</th>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>6.7</td>\n    </tr>\n    <tr>\n      <th>USA</th>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>3.9</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">Europe</th>\n      <th>Germany</th>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>Italy</th>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.252208500Z",
     "start_time": "2025-09-11T01:55:12.440362600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "outputs": [
    {
     "data": {
      "text/plain": "properties continent  user_id   name  age  score\ncountry                                         \nUSA          America     1000   John   33    6.7\nUSA          America     1002    Tim   41    3.9\nGermany       Europe     1003  Jenny   12    9.0\nItaly         Europe     1001   Mark   55    4.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>continent</th>\n      <th>user_id</th>\n      <th>name</th>\n      <th>age</th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>country</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>USA</th>\n      <td>America</td>\n      <td>1000</td>\n      <td>John</td>\n      <td>33</td>\n      <td>6.7</td>\n    </tr>\n    <tr>\n      <th>USA</th>\n      <td>America</td>\n      <td>1002</td>\n      <td>Tim</td>\n      <td>41</td>\n      <td>3.9</td>\n    </tr>\n    <tr>\n      <th>Germany</th>\n      <td>Europe</td>\n      <td>1003</td>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>9.0</td>\n    </tr>\n    <tr>\n      <th>Italy</th>\n      <td>Europe</td>\n      <td>1001</td>\n      <td>Mark</td>\n      <td>55</td>\n      <td>4.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_multi.reset_index(level=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.252208500Z",
     "start_time": "2025-09-11T01:55:12.470346Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent\nuser_id                                         \n1001         Mark   55    Italy    4.5    Europe\n1000         John   33      USA    6.7   America\n1002          Tim   41      USA    3.9   America\n1003        Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.253207400Z",
     "start_time": "2025-09-11T01:55:12.500939800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "outputs": [],
   "source": [
    "df2 = df.copy()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.254206800Z",
     "start_time": "2025-09-11T01:55:12.533848600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "outputs": [],
   "source": [
    "df2.loc[1000, 'name'] = 'JOHN'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.254206800Z",
     "start_time": "2025-09-11T01:55:12.550838400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent\nuser_id                                         \n1001         Mark   55    Italy    4.5    Europe\n1000         JOHN   33      USA    6.7   America\n1002          Tim   41      USA    3.9   America\n1003        Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>JOHN</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.255206100Z",
     "start_time": "2025-09-11T01:55:12.579821700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3\n0   300.1   400.3  1000.5\n1   100.2   300.4  1100.6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>300.1</td>\n      <td>400.3</td>\n      <td>1000.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>100.2</td>\n      <td>300.4</td>\n      <td>1100.6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.256205700Z",
     "start_time": "2025-09-11T01:55:12.612803Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "outputs": [],
   "source": [
    "rainfall2 = rainfall.copy()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.257205100Z",
     "start_time": "2025-09-11T01:55:12.643785100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3\n0   300.1   400.3  1000.5\n1   100.2   300.4  1100.6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>300.1</td>\n      <td>400.3</td>\n      <td>1000.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>100.2</td>\n      <td>300.4</td>\n      <td>1100.6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.257205100Z",
     "start_time": "2025-09-11T01:55:12.660775Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "outputs": [],
   "source": [
    "rainfall2[rainfall2 < 400] = 0"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.258204200Z",
     "start_time": "2025-09-11T01:55:12.724510700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3\n0     0.0   400.3  1000.5\n1     0.0     0.0  1100.6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.0</td>\n      <td>400.3</td>\n      <td>1000.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1100.6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.259203800Z",
     "start_time": "2025-09-11T01:55:12.740501600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  name   age country  score continent\nuser_id                                        \n1001        Mark  55.0   Italy    4.5    Europe\n1000        John  33.0     USA    NaN   America\n1002         Tim  41.0     USA    3.9   America\n1003        None   NaN    None    NaN      None",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55.0</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33.0</td>\n      <td>USA</td>\n      <td>NaN</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41.0</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>None</td>\n      <td>NaN</td>\n      <td>None</td>\n      <td>NaN</td>\n      <td>None</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.copy()  # 从一个新的副本开始\n",
    "df2.loc[1000, \"score\"] = None\n",
    "df2.loc[1003, :] = None\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.259203800Z",
     "start_time": "2025-09-11T01:55:12.771484100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  name   age country  score continent\nuser_id                                        \n1001        Mark  55.0   Italy    4.5    Europe\n1000        John  33.0     USA    4.2   America\n1002         Tim  41.0     USA    3.9   America\n1003        None   NaN    None    4.2      None",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55.0</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33.0</td>\n      <td>USA</td>\n      <td>4.2</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41.0</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>None</td>\n      <td>NaN</td>\n      <td>None</td>\n      <td>4.2</td>\n      <td>None</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.fillna({'score': df2['score'].mean()})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.260204300Z",
     "start_time": "2025-09-11T01:55:12.802466400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "outputs": [
    {
     "data": {
      "text/plain": "user_id\n1001    False\n1000    False\n1002     True\n1003    False\nName: country, dtype: bool"
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"country\"].duplicated()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.260204300Z",
     "start_time": "2025-09-11T01:55:12.835453Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  name  age country  score continent\nuser_id                                       \n1000        John   33     USA    6.7   America\n1002         Tim   41     USA    3.9   America",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[df[\"country\"].duplicated(keep=False), :]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.262201700Z",
     "start_time": "2025-09-11T01:55:12.868430200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3\n0   300.1   400.3  1000.5\n1   100.2   300.4  1100.6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>300.1</td>\n      <td>400.3</td>\n      <td>1000.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>100.2</td>\n      <td>300.4</td>\n      <td>1100.6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.262201700Z",
     "start_time": "2025-09-11T01:55:12.899410900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3\n0   400.1   500.3  1100.5\n1   200.2   400.4  1200.6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>400.1</td>\n      <td>500.3</td>\n      <td>1100.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>200.2</td>\n      <td>400.4</td>\n      <td>1200.6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall + 100"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.262201700Z",
     "start_time": "2025-09-11T01:55:12.934390700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "outputs": [],
   "source": [
    "more_rainfall = pd.DataFrame(data=[[100, 200], [300, 400]],\n",
    "                             index=[1, 2],\n",
    "                             columns=[\"City 1\", \"City 4\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.263201600Z",
     "start_time": "2025-09-11T01:55:12.981362500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 4\n1     100     200\n2     300     400",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 4</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>100</td>\n      <td>200</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>300</td>\n      <td>400</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "more_rainfall"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.263201600Z",
     "start_time": "2025-09-11T01:55:13.009347100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3  City 4\n0     NaN     NaN     NaN     NaN\n1   200.2     NaN     NaN     NaN\n2     NaN     NaN     NaN     NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n      <th>City 4</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>200.2</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall + more_rainfall"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.263201600Z",
     "start_time": "2025-09-11T01:55:13.055320500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "outputs": [
    {
     "data": {
      "text/plain": "   City 1  City 2  City 3  City 4\n0   300.1   400.3  1000.5     NaN\n1   200.2   300.4  1100.6   200.0\n2   300.0     NaN     NaN   400.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City 1</th>\n      <th>City 2</th>\n      <th>City 3</th>\n      <th>City 4</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>300.1</td>\n      <td>400.3</td>\n      <td>1000.5</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>200.2</td>\n      <td>300.4</td>\n      <td>1100.6</td>\n      <td>200.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>300.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>400.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rainfall.add(more_rainfall, fill_value=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.264201200Z",
     "start_time": "2025-09-11T01:55:13.087303100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "outputs": [],
   "source": [
    "selection = df.loc[:, ['country', 'continent']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.264201200Z",
     "start_time": "2025-09-11T01:55:13.121283100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  country continent\nuser_id                      \n1001          Italy    Europe\n1000            USA   America\n1002            USA   America\n1003        Germany    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>country</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Italy</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>USA</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>USA</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Germany</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "selection"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.265201800Z",
     "start_time": "2025-09-11T01:55:13.135275100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent\nuser_id                                         \n1001         Mark   55    Italy    4.5    Europe\n1000         John   33      USA    6.7   America\n1002          Tim   41      USA    3.9   America\n1003        Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.823506300Z",
     "start_time": "2025-09-11T01:55:13.164258600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "outputs": [],
   "source": [
    "df['1000', 'name'] = 'Wuj'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.824505500Z",
     "start_time": "2025-09-11T01:55:13.201241300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent (1000, name)\nuser_id                                                      \n1001         Mark   55    Italy    4.5    Europe          Wuj\n1000         John   33      USA    6.7   America          Wuj\n1002          Tim   41      USA    3.9   America          Wuj\n1003        Jenny   12  Germany    9.0    Europe          Wuj",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n      <th>(1000, name)</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n      <td>Wuj</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n      <td>Wuj</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n      <td>Wuj</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n      <td>Wuj</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.824505500Z",
     "start_time": "2025-09-11T01:55:13.222226700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "outputs": [
    {
     "data": {
      "text/plain": "properties  country continent\nuser_id                      \n1001          Italy    Europe\n1000            USA   America\n1002            USA   America\n1003        Germany    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>country</th>\n      <th>continent</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Italy</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>USA</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>USA</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Germany</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "selection"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.827502900Z",
     "start_time": "2025-09-11T01:55:13.244213600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "outputs": [
    {
     "data": {
      "text/plain": "user_id\n1001    Wuj\n1000    Wuj\n1002    Wuj\n1003    Wuj\nName: (1000, name), dtype: object"
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['1000', 'name']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.959718200Z",
     "start_time": "2025-09-11T01:55:13.275194700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "outputs": [
    {
     "data": {
      "text/plain": "properties   name  age  country  score continent (1000, name)\nuser_id                                                      \n1001         Mark   55    Italy    4.5    Europe          Wuj\n1000         John   33      USA    6.7   America          Wuj\n1002          Tim   41      USA    3.9   America          Wuj\n1003        Jenny   12  Germany    9.0    Europe          Wuj",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>properties</th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n      <th>(1000, name)</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n      <td>Wuj</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n      <td>Wuj</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n      <td>Wuj</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n      <td>Wuj</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:13.995698900Z",
     "start_time": "2025-09-11T01:55:13.292185500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "The following keys are not valid labels or levels for axis 0: ['1000,name']",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[160], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mdf\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_drop_labels_or_levels\u001B[49m\u001B[43m(\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43m1000,name\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mD:\\PythonStudy\\excel+python\\venv\\lib\\site-packages\\pandas\\core\\generic.py:1967\u001B[0m, in \u001B[0;36mNDFrame._drop_labels_or_levels\u001B[1;34m(self, keys, axis)\u001B[0m\n\u001B[0;32m   1962\u001B[0m invalid_keys \u001B[38;5;241m=\u001B[39m [\n\u001B[0;32m   1963\u001B[0m     k \u001B[38;5;28;01mfor\u001B[39;00m k \u001B[38;5;129;01min\u001B[39;00m keys \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_is_label_or_level_reference(k, axis\u001B[38;5;241m=\u001B[39maxis)\n\u001B[0;32m   1964\u001B[0m ]\n\u001B[0;32m   1966\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m invalid_keys:\n\u001B[1;32m-> 1967\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[0;32m   1968\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mThe following keys are not valid labels or \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   1969\u001B[0m         \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mlevels for axis \u001B[39m\u001B[38;5;132;01m{\u001B[39;00maxis\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00minvalid_keys\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   1970\u001B[0m     )\n\u001B[0;32m   1972\u001B[0m \u001B[38;5;66;03m# Compute levels and labels to drop\u001B[39;00m\n\u001B[0;32m   1973\u001B[0m levels_to_drop \u001B[38;5;241m=\u001B[39m [k \u001B[38;5;28;01mfor\u001B[39;00m k \u001B[38;5;129;01min\u001B[39;00m keys \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_is_level_reference(k, axis\u001B[38;5;241m=\u001B[39maxis)]\n",
      "\u001B[1;31mValueError\u001B[0m: The following keys are not valid labels or levels for axis 0: ['1000,name']"
     ]
    }
   ],
   "source": [
    "df._drop_labels_or_levels(['1000,name'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:14.421402900Z",
     "start_time": "2025-09-11T01:55:13.322168400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.401070900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.drop(columns=['1000,name'], axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.405068700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "data = [[15, \"France\", 4.1, \"Becky\"],\n",
    "        [44, \"Canada\", 6.1, \"Leanne\"]]\n",
    "more_users = pd.DataFrame(data=data,\n",
    "                          columns=[\"age\", \"country\", \"score\", \"name\"],\n",
    "                          index=[1000, 1011])\n",
    "more_users"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.409065200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.416061300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "pd.concat([df, more_users], axis=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.421059100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.426056Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "outputs": [
    {
     "data": {
      "text/plain": "       name  age  country  score continent\n1001   Mark   55    Italy    4.5    Europe\n1000   John   33      USA    6.7   America\n1002    Tim   41      USA    3.9   America\n1003  Jenny   12  Germany    9.0    Europe",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n      <th>country</th>\n      <th>score</th>\n      <th>continent</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1001</th>\n      <td>Mark</td>\n      <td>55</td>\n      <td>Italy</td>\n      <td>4.5</td>\n      <td>Europe</td>\n    </tr>\n    <tr>\n      <th>1000</th>\n      <td>John</td>\n      <td>33</td>\n      <td>USA</td>\n      <td>6.7</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1002</th>\n      <td>Tim</td>\n      <td>41</td>\n      <td>USA</td>\n      <td>3.9</td>\n      <td>America</td>\n    </tr>\n    <tr>\n      <th>1003</th>\n      <td>Jenny</td>\n      <td>12</td>\n      <td>Germany</td>\n      <td>9.0</td>\n      <td>Europe</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [[\"Mark\", 55, \"Italy\", 4.5, \"Europe\"],\n",
    "        [\"John\", 33, \"USA\", 6.7, \"America\"],\n",
    "        [\"Tim\", 41, \"USA\", 3.9, \"America\"],\n",
    "        [\"Jenny\", 12, \"Germany\", 9.0, \"Europe\"]]\n",
    "df = pd.DataFrame(data=data,\n",
    "                  columns=[\"name\", \"age\", \"country\",\n",
    "                           \"score\", \"continent\"],\n",
    "                  index=[1001, 1000, 1002, 1003])\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:48.937346900Z",
     "start_time": "2025-09-11T01:55:48.871385600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.groupby(['continent']).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.440048600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.445045500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.groupby([\"continent\", \"country\"]).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.453040700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "data = [[\"Oranges\", \"North\", 12.30],\n",
    "        [\"Apples\", \"South\", 10.55],\n",
    "        [\"Oranges\", \"South\", 22.00],\n",
    "        [\"Bananas\", \"South\", 5.90],\n",
    "        [\"Bananas\", \"North\", 31.30],\n",
    "        [\"Oranges\", \"North\", 13.10]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.460036800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "sales = pd.DataFrame(data=data,\n",
    "                     columns=[\"Fruit\", \"Region\", \"Revenue\"])\n",
    "sales"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.468031900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.475028400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "data = [[\"Oranges\", \"North\", 12.30],\n",
    "        [\"Apples\", \"South\", 10.55],\n",
    "        [\"Oranges\", \"South\", 22.00],\n",
    "        [\"Bananas\", \"South\", 5.90],\n",
    "        [\"Bananas\", \"North\", 31.30],\n",
    "        [\"Oranges\", \"North\", 13.10]]\n",
    "sales = pd.DataFrame(data=data,\n",
    "                     columns=[\"Fruit\", \"Region\", \"Revenue\"])\n",
    "sales\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.480025300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.487949700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.groupby(['continent', 'country']).mean('score')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2025-09-11T01:55:13.495944700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "outputs": [
    {
     "data": {
      "text/plain": "     Fruit Region  Revenue\n0  Oranges  North    12.30\n1   Apples  South    10.55\n2  Oranges  South    22.00\n3  Bananas  South     5.90\n4  Bananas  North    31.30\n5  Oranges  North    13.10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Fruit</th>\n      <th>Region</th>\n      <th>Revenue</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Oranges</td>\n      <td>North</td>\n      <td>12.30</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Apples</td>\n      <td>South</td>\n      <td>10.55</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Oranges</td>\n      <td>South</td>\n      <td>22.00</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Bananas</td>\n      <td>South</td>\n      <td>5.90</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Bananas</td>\n      <td>North</td>\n      <td>31.30</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>Oranges</td>\n      <td>North</td>\n      <td>13.10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [[\"Oranges\", \"North\", 12.30],\n",
    "        [\"Apples\", \"South\", 10.55],\n",
    "        [\"Oranges\", \"South\", 22.00],\n",
    "        [\"Bananas\", \"South\", 5.90],\n",
    "        [\"Bananas\", \"North\", 31.30],\n",
    "        [\"Oranges\", \"North\", 13.10]]\n",
    "sales = pd.DataFrame(data=data,\n",
    "                     columns=[\"Fruit\", \"Region\", \"Revenue\"])\n",
    "sales"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:55:58.306168Z",
     "start_time": "2025-09-11T01:55:58.237187500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "outputs": [
    {
     "data": {
      "text/plain": "     Fruit Region  Revenue\n0  Oranges  North    12.30\n1   Apples  South    10.55\n2  Oranges  South    22.00\n3  Bananas  South     5.90\n4  Bananas  North    31.30\n5  Oranges  North    13.10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Fruit</th>\n      <th>Region</th>\n      <th>Revenue</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Oranges</td>\n      <td>North</td>\n      <td>12.30</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Apples</td>\n      <td>South</td>\n      <td>10.55</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Oranges</td>\n      <td>South</td>\n      <td>22.00</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Bananas</td>\n      <td>South</td>\n      <td>5.90</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Bananas</td>\n      <td>North</td>\n      <td>31.30</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>Oranges</td>\n      <td>North</td>\n      <td>13.10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:56:00.472213300Z",
     "start_time": "2025-09-11T01:56:00.399870900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "outputs": [],
   "source": [
    "pivot = pd.pivot_table(sales, index='Fruit', columns='Region', values='Revenue', aggfunc='sum', margins=True,\n",
    "                       margins_name='Total')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:56:03.110608400Z",
     "start_time": "2025-09-11T01:56:03.041366400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "outputs": [
    {
     "data": {
      "text/plain": "Region   North  South  Total\nFruit                       \nApples     NaN  10.55  10.55\nBananas   31.3   5.90  37.20\nOranges   25.4  22.00  47.40\nTotal     56.7  38.45  95.15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Region</th>\n      <th>North</th>\n      <th>South</th>\n      <th>Total</th>\n    </tr>\n    <tr>\n      <th>Fruit</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Apples</th>\n      <td>NaN</td>\n      <td>10.55</td>\n      <td>10.55</td>\n    </tr>\n    <tr>\n      <th>Bananas</th>\n      <td>31.3</td>\n      <td>5.90</td>\n      <td>37.20</td>\n    </tr>\n    <tr>\n      <th>Oranges</th>\n      <td>25.4</td>\n      <td>22.00</td>\n      <td>47.40</td>\n    </tr>\n    <tr>\n      <th>Total</th>\n      <td>56.7</td>\n      <td>38.45</td>\n      <td>95.15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:56:05.196694100Z",
     "start_time": "2025-09-11T01:56:04.855454800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "outputs": [
    {
     "data": {
      "text/plain": "     Fruit Region  Revenue\n0  Oranges  North    12.30\n1   Apples  South    10.55\n2  Oranges  South    22.00\n3  Bananas  South     5.90\n4  Bananas  North    31.30\n5  Oranges  North    13.10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Fruit</th>\n      <th>Region</th>\n      <th>Revenue</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Oranges</td>\n      <td>North</td>\n      <td>12.30</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Apples</td>\n      <td>South</td>\n      <td>10.55</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Oranges</td>\n      <td>South</td>\n      <td>22.00</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Bananas</td>\n      <td>South</td>\n      <td>5.90</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Bananas</td>\n      <td>North</td>\n      <td>31.30</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>Oranges</td>\n      <td>North</td>\n      <td>13.10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:56:07.553626100Z",
     "start_time": "2025-09-11T01:56:07.413486500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "outputs": [
    {
     "data": {
      "text/plain": "Region   North  South\nFruit                \nApples     NaN  10.55\nBananas   31.3   5.90\nOranges   25.4  22.00",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Region</th>\n      <th>North</th>\n      <th>South</th>\n    </tr>\n    <tr>\n      <th>Fruit</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Apples</th>\n      <td>NaN</td>\n      <td>10.55</td>\n    </tr>\n    <tr>\n      <th>Bananas</th>\n      <td>31.3</td>\n      <td>5.90</td>\n    </tr>\n    <tr>\n      <th>Oranges</th>\n      <td>25.4</td>\n      <td>22.00</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot.iloc[:-1, :-1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:56:32.882692100Z",
     "start_time": "2025-09-11T01:56:32.756914700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "outputs": [
    {
     "data": {
      "text/plain": "Region   North  South  Total\nFruit                       \nApples     NaN  10.55  10.55\nBananas   31.3   5.90  37.20\nOranges   25.4  22.00  47.40\nTotal     56.7  38.45  95.15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Region</th>\n      <th>North</th>\n      <th>South</th>\n      <th>Total</th>\n    </tr>\n    <tr>\n      <th>Fruit</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Apples</th>\n      <td>NaN</td>\n      <td>10.55</td>\n      <td>10.55</td>\n    </tr>\n    <tr>\n      <th>Bananas</th>\n      <td>31.3</td>\n      <td>5.90</td>\n      <td>37.20</td>\n    </tr>\n    <tr>\n      <th>Oranges</th>\n      <td>25.4</td>\n      <td>22.00</td>\n      <td>47.40</td>\n    </tr>\n    <tr>\n      <th>Total</th>\n      <td>56.7</td>\n      <td>38.45</td>\n      <td>95.15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:56:47.696961200Z",
     "start_time": "2025-09-11T01:56:47.592015900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "outputs": [
    {
     "data": {
      "text/plain": "Region    Fruit  North  South\n0        Apples    NaN  10.55\n1       Bananas   31.3   5.90\n2       Oranges   25.4  22.00",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Region</th>\n      <th>Fruit</th>\n      <th>North</th>\n      <th>South</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Apples</td>\n      <td>NaN</td>\n      <td>10.55</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Bananas</td>\n      <td>31.3</td>\n      <td>5.90</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Oranges</td>\n      <td>25.4</td>\n      <td>22.00</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot.iloc[:-1, :-1].reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:57:29.675442700Z",
     "start_time": "2025-09-11T01:57:29.533524300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "outputs": [
    {
     "data": {
      "text/plain": "     Fruit Region  Revenue\n0   Apples  North      NaN\n1  Bananas  North    31.30\n2  Oranges  North    25.40\n3   Apples  South    10.55\n4  Bananas  South     5.90\n5  Oranges  South    22.00",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Fruit</th>\n      <th>Region</th>\n      <th>Revenue</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Apples</td>\n      <td>North</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Bananas</td>\n      <td>North</td>\n      <td>31.30</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Oranges</td>\n      <td>North</td>\n      <td>25.40</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Apples</td>\n      <td>South</td>\n      <td>10.55</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Bananas</td>\n      <td>South</td>\n      <td>5.90</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>Oranges</td>\n      <td>South</td>\n      <td>22.00</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.melt(pivot.iloc[:-1, :-1].reset_index(),\n",
    "        id_vars=\"Fruit\",\n",
    "        value_vars=[\"North\", \"South\"], value_name=\"Revenue\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-11T01:59:16.862619500Z",
     "start_time": "2025-09-11T01:59:16.756679800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
